package com.hs.mpdemo_01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.mpdemo_01.entity.Users;
import com.hs.mpdemo_01.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService service;

    @GetMapping
    public List<Users> fidnAll(){
        return service.findAll();
    }

    @GetMapping("/page/{pageNum}/{pageSize}")
    public HashMap<String,Object> findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        Page<Users> usersPage = service.fidnByPage(pageNum, pageSize);
        HashMap<String,Object> userMap = new HashMap<>();
        userMap.put("list",usersPage.getRecords());
        userMap.put("total",usersPage.getTotal());
        return userMap;
    }

    @PostMapping
    public String addUser(@RequestBody Users users){
        service.addUser(users);
        return "添加成功";
    }

    @PutMapping
    public String updateUser(@RequestBody Users users){
        int updateUser = service.updateUser(users);
        return "更新成功";
    }
}
